package com.tjf.skin_loading;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Outline;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.os.Build;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewOutlineProvider;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

/**
 * 跑马灯效果
 * <p>
 * 出处
 * https://mp.weixin.qq.com/s/GGSCW4Xe2h36txIVFIXEIQ
 */
public class ApertureViewGroup extends LinearLayout {

    int def_width = 200;
    int def_height = def_width;

    Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);//实例化画笔 抗锯齿

    private int mColor1 = 0;
    private int mColor2 = 0;

    // 边框宽度
    private float mBorderWidth = 0f;

    // 边框角度
    private float mBorderAngle = 0f;

    // 光圈速度
    private int mDuration = 0;

    // 中间颜色
    private int mMiddleColor = 0;

    public ApertureViewGroup(Context context) {
        super(context);
        init(context, null);
    }

    public ApertureViewGroup(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ApertureViewGroup(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    private void init(Context context, AttributeSet attrs) {

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ApertureViewGroup);
        try {
            mColor1 = a.getColor(R.styleable.ApertureViewGroup_aperture_color1, Color.YELLOW);
            mColor2 = a.getColor(R.styleable.ApertureViewGroup_aperture_color2, -1);
            mBorderWidth =
                    a.getDimension(R.styleable.ApertureViewGroup_aperture_border_width,
                            20);

//            setPadding( mBorderWidth  / 2)
            setPadding(20, 0, 20, 0);
            mBorderAngle =
                    a.getDimension(R.styleable.ApertureViewGroup_aperture_border_angle,
                            20);

            mDuration = a.getInt(R.styleable.ApertureViewGroup_aperture_duration,
                    3000);

            mMiddleColor =
                    a.getColor(R.styleable.ApertureViewGroup_aperture_middle_color,
                            Color.BLACK);


        } finally {
            a.recycle();
        }
        setPath();
        setAnimator();
        setBgRadius(this, mBorderAngle);
    }

    /**
     * 设置布局圆角
     *
     * @param layoutView
     * @param mRadius
     */
    private void setBgRadius(View layoutView, float mRadius) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            layoutView.setOutlineProvider(new ViewOutlineProvider() {
                @Override
                public void getOutline(View view, Outline outline) {
                    outline.setRoundRect(0, 0,
                            view.getWidth(),
                            view.getHeight(), mRadius);
                }
            });
            //设置阴影
//            layoutView.setElevation(10f);
            //圆角裁切
            layoutView.setClipToOutline(true);
        }
    }

    ObjectAnimator animator;
    private float currentSpeed = 0f;

    public void setCurrentSpeed(float mCurrentSpeed) {
        currentSpeed = mCurrentSpeed;
        invalidate();
    }

    void setAnimator() {
        animator = ObjectAnimator.ofFloat(this, "currentSpeed", 0f, 360f);
        animator.setRepeatCount(-1);
        animator.setRepeatMode(ObjectAnimator.RESTART);
        animator.setInterpolator(null);
        animator.setDuration(mDuration);

    }


    public LinearGradient color1() {
        LinearGradient linearGradient = new LinearGradient(
                def_width * 1f,
                def_height / 2f,
                def_width * 1f,
                def_height * 1f,
                new int[]{Color.TRANSPARENT, mColor1},
                new float[]{0f, 0.9f},
                Shader.TileMode.CLAMP);

        return linearGradient;
    }

    public LinearGradient color2() {
        LinearGradient linearGradient = new LinearGradient(
                def_width / 2f,
                def_height / 2f,
                def_width / 2f,
                0f,
                new int[]{Color.TRANSPARENT, mColor2},
                new float[]{0f, 0.9f},
                Shader.TileMode.CLAMP);

        return linearGradient;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        def_width = resolveSize(def_width, widthMeasureSpec);
        def_height = resolveSize(def_height, heightMeasureSpec);
        setMeasuredDimension(def_width, def_height);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        animator.start();

    }



    @Override
    protected void dispatchDraw(Canvas canvas) {


        canvas.save();//保存画布  保存的初始状态

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            canvas.clipOutPath(path);
        }
        //进行旋转 根据角度 通过属性动画
        canvas.rotate(currentSpeed, def_width / 2f, def_height / 2f);
        float left = def_width / 2f;
        float right = left + def_width;
        float top = def_height / 2f;
        float buttom = top + def_height / 2f;
        // 绘制渐变view1
        mPaint.setShader(color1());
        canvas.drawRect(left, top, right, buttom, mPaint);
        mPaint.setShader(null);
        if (mColor2 != -1) {
            // 绘制渐变view2
            mPaint.setShader(color2());
            canvas.drawRect(left, top, -right, -buttom, mPaint);
            mPaint.setShader(null);
        }
        canvas.restore();//恢复画布  取出原来保存的状态
        //布局里面的圆角矩形
        mPaint.setColor(mMiddleColor);
        canvas.drawRoundRect(rectF(), mBorderAngle, mBorderAngle, mPaint);
        super.dispatchDraw(canvas);
    }


    public RectF rectF() {
        float left = 0 + mBorderWidth / 2f;
        float top = 0 + mBorderWidth / 2f;
        float right = left + def_width - mBorderWidth;
        float buttom = top + def_height - mBorderWidth;

        return new RectF(left, top, right, buttom);
    }

    Path path;

    public void setPath() {
        path = new Path();
        path.addRoundRect(rectF(), mBorderWidth,
                mBorderWidth, Path.Direction.CCW);
    }


}
